/*
 * Copyright 2013 Agustina Martinez-Garcia

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package uk.ljmu.mulgaraifz.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class CollectionNode implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -7272467961553870731L;

    private String title;

    /**
     * @return the title
     */
    public String getTitle() {
	return title;
    }

    /**
     * @return the title
     */
    public void setTitle(String value) {
	title = value;
    }

    private boolean currentChild;

    /**
     * @return the currentChild
     */
    public boolean isCurrentChild() {
	return currentChild;
    }

    /**
     * @param currentChild
     *            the currentChild to set
     */
    public void setCurrentChild(boolean currentChild) {
	this.currentChild = currentChild;
    }

    /**
     * @return the currentChild
     */
    public boolean getCurrentChild(String id) {
	currentChild = false;
	this.hasChildNode(this, id);
	return currentChild;
    }

    /** List of children, Object type : CustomNode */
    private List<CollectionNode> children;

    /** Text value of the node, readable ID **/
    private String value;

    /** Fedora ID for extracting information about a particular node **/
    private String wildcardID;

    private boolean isCollection;

    public void setIsCollection(boolean value) {
	isCollection = value;
    }

    public boolean getIsCollection() {
	return isCollection;
    }

    public void setWildcardID(String wildcardID) {
	this.wildcardID = wildcardID;
    }

    public String getWildcardID() {
	return wildcardID;
    }

    /**
     * @param children
     *            the children to set
     */
    public void setChildren(List<CollectionNode> children) {
	this.children = children;
    }

    /** Variable to store if this object has any child */
    private boolean child;

    /**
     * @param child
     *            the child to set
     */
    public void setChild(boolean child) {
	this.child = child;
    }

    /** Returns true if this object has any children */
    public boolean hasChild() {
	return child;
    }

    /** Returns List of children */
    public List<CollectionNode> getChildren() {
	return children;
    }

    /** Returns value to displayed */
    public String getValue() {
	return value;
    }

    public CollectionNode(List<CollectionNode> childrenList) {
	this.value = "Root node";
	this.setChild(true);
	this.setChildren(childrenList);
	currentChild = false;
    }

    public CollectionNode(String val, boolean child) {
	this.value = val;
	this.setChild(child);
	this.children = new ArrayList<CollectionNode>();
	currentChild = false;
    }

    public CollectionNode(String val, boolean child, boolean collection) {
	this.value = val;
	this.setChild(child);
	this.children = new ArrayList<CollectionNode>();
	isCollection = collection;
	currentChild = false;
    }

    public void hasChildNode(CollectionNode currentNode, String id) {
	for (CollectionNode child : currentNode.getChildren()) {
	    if (child.getValue().equalsIgnoreCase(id)) {
		currentChild = true;
	    }
	    if (child.hasChild()) {
		hasChildNode(child, id);
	    }
	}
    }
}